package nl.hva.three2learn.model;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Table(name = "course")
@Entity
public class Course implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3055444804025938186L;

	@Id
	@GeneratedValue
	@Column(name = "course_id")
	private long id;

	@Column(name = "name")
	private String name;

	@Column(name = "description")
	private String description;

	@Column(name = "price")
	private double price;

	@ManyToOne
	private Category category;

	@OrderColumn
	@OneToMany
	@LazyCollection(LazyCollectionOption.FALSE)
	private List<Topic> topics;

	@OrderColumn
	@OneToMany
@LazyCollection(LazyCollectionOption.FALSE)
	private List<Chapter> chapters;

	@Column(name = "is_disabled")
	private boolean isDisabled;

	public Course() {
	}

	public Course(String name, String description, Date start, long duration,
			double price, Category category) {
		this.name = name;
		this.description = description;
		topics = new LinkedList<Topic>();

		this.price = price;

		this.category = category;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public double getPrice() {
		return price;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public List<Topic> getTopics() {
		return topics;
	}

	public Topic getTopicById(long topicId) {
		Topic toBeReturned = null;
		List<Topic> temp = getTopics();
		for (int i = 0; i < temp.size(); i++) {
			Topic current = temp.get(i);
			if (current.getId() == topicId) {
				toBeReturned = current;
				break;
			}
		}
		return toBeReturned;
	}

	public void addTopic(Topic toBeAdded) {
		topics.add(toBeAdded);
	}


	public void setTopics(List<Topic> topics) {
		this.topics = topics;
	}

	public List<Chapter> getChapters() {
		return chapters;
	}

	public void setChapters(List<Chapter> chapters) {
		this.chapters = chapters;
	}

	public boolean isDisabled() {
		return isDisabled;
	}

	public void setDisabled(boolean isDisabled) {
		this.isDisabled = isDisabled;
	}
}
